/**
 * Created by admin on 2016/12/14.
 */


'use strict'

let mor = require("../libs/mongo/mongoHelper");
let querystring = require('querystring');
let dbr = require("../libs/mysql/mysqlHelper");
let jv = require("../libs/sys/myHelper").jv;
let http = require('request-promise');

app.use(app.routes.get('/html/message/list', async ctx => {
  ctx.render('message/list');
}));

app.use(app.routes.get('/html/message/detail', async ctx => {
  ctx.render('message/detail');
}));


app.use(app.routes.post('/message/list', async ctx => {
  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }


  let skip = ctx.req.json.skip;
  let take = ctx.req.json.take;

  let results = await dbr.find(`select id,title , content,create_date createAt ,fuwu_name fuwuName
from thwy_message
where receiver_id = :userId and del_flag = 0
order by create_date desc
limit :skip,:take `,
    {userId: userId, skip: skip, take: take});

  if (!results || !results.length) {
    ctx.body = [];
    return;
  }

  ctx.body = results;
}));


app.use(app.routes.post('/message/detail', async ctx => {
  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }
  let msgId = ctx.req.json.msgId;
  if (!msgId) {
    ctx.msg = "找不到消息ID";
    return;
  }

  let results = await dbr.findOne(`select id,title,content,create_date createAt ,fuwu_name fuwuName
from thwy_message
where id = :msgId and receiver_id =:userId`,
    {msgId: msgId, userId: userId});

  if (!results) {
    return;
  }

  ctx.body = results;
}));
